﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.common;
using System.IO;
using rock.UI.common;

namespace rock.UI.system
{
    public partial class Form1Database : Form
    {
        public Form1Database()
        {
            InitializeComponent();


        }
        string IpAddress;
        string DataBaseName;
        string Username;
        string Password;
        string ConnectionString;

        private void Form1Database_Load(object sender, EventArgs e)
        {
           
            String sqlCon = ConfigUtils.GetConnectionStringsConfig("rock.Properties.Settings.RentalHousingManagerConnectionString");
            String[] data = sqlCon.Split(new char[2] { ';', '=' });

            this.IpAddress = data[1];
            this.DataBaseName = data[3];
            this.Username = data[5];
            this.Password = data[7];
            ConnectionString = "Data Source=" + this.IpAddress + ";User id=" + this.Username + ";Password=" + this.Password + "; Initial Catalog=master"; 

        }
        //ALTER DATABASE [datebase] SET OFFLINE WITH ROLLBACK IMMEDIATE
　　　  //还原数据库 
        private void button0_Click(object sender, EventArgs e) 
        {

            //this.openFileDialog1.ShowDialog();

            DialogResult dr = this.openFileDialog1.ShowDialog();
            
            
            if (dr == DialogResult.OK)
            {
                 String fileName =  openFileDialog1.SafeFileName;
                 String filePath = openFileDialog1.FileName.Replace(fileName , "");

                 MessageBox.Show(fileName);
                 MessageBox.Show(filePath);
                 DataBaseControl DBC = new DataBaseControl();


                 DBC.ConnectionString = ConnectionString;
                 DBC.DataBaseName = DataBaseName;
                 DBC.DataBaseOfBackupName = fileName; //@back.bak
                 DBC.DataBaseOfBackupPath = filePath;// @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";
                 DBC.ReplaceDataBase(); 
            }


           
        } 

　　　　       
       
　　　　        //备份数据库 
        private void button2_Click(object sender, EventArgs e) 
        { 
           SaveFileDialog saveFileDialog=new SaveFileDialog();　
            saveFileDialog.Filter="备份文件|*.bak|所有文件|*.*";　
            saveFileDialog.FilterIndex=0;　
            saveFileDialog.RestoreDirectory=true;

            String database_bak_path = ConfigUtils.GetAppConfig("database_bak_path");
            saveFileDialog.FileName = database_bak_path + "\\" + DateTime.Now.ToString(StringUtils.YYYYMMDD) + "数据库备份文件" + ".bak";

            if(saveFileDialog.ShowDialog()==DialogResult.OK)　
            {　
                String fName=saveFileDialog.FileName;

                int index = fName.LastIndexOf("\\") + 1;
                String name = fName.Substring(index, fName.Length - index);
                String path = fName.Substring(0, index);
 
                DataBaseControl DBC = new DataBaseControl();
                DBC.ConnectionString = ConnectionString;
                DBC.DataBaseName = DataBaseName;
                DBC.DataBaseOfBackupName = name;//@"back.bak";
                DBC.DataBaseOfBackupPath = path; //@"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";
                DBC.BackupDataBase();
                 
            }
        }


        //附加数据库 
        private void button1_Click_1(object sender, EventArgs e)
        {
            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                String filePathName = openFileDialog1.FileName;
                DataBaseControl DBC = new DataBaseControl();
                DBC.ConnectionString = ConnectionString;
                DBC.DataBaseName = DataBaseName;
                DBC.DataBase_MDF = filePathName;// @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/MyDatabase_Data.MDF";
                DBC.DataBase_LDF = filePathName.Replace(".MDF", "_Log.LDF");// @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/MyDatabase_Log.LDF";
                DBC.AddDataBase();
            }
        } 

　　　　        //分离数据库 
        private void button3_Click(object sender, EventArgs e) 
        { 
            DataBaseControl DBC = new DataBaseControl();
            DBC.ConnectionString = ConnectionString;
            DBC.DataBaseName = DataBaseName; 
            DBC.DeleteDataBase(); 
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //Data Source=192.168.1.102,1433;Initial Catalog=RentalHousingManager;User ID=sa;Password=access2008;Integrated Security=SSPI;Connection Timeout=5
            String sqlCon = ConfigUtils.GetConnectionStringsConfig("rock.Properties.Settings.RentalHousingManagerConnectionString");
            String[] data = sqlCon.Split(new char[2] { ';', '=' });

            String ipdata = data[1];
            String db = data[3];
            String user = data[5];
            String pwd = data[7];

           // MessageBox.Show(ipport.Length+"");

            //for (int i = 0; i < ipport.Length; i++) { 
            
            //    MessageBox.Show(ipport[i]);
            //}
        }

       
    }
}
